#include "Items.h"
#include "Resource.h"

Items::Items(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State) : Object(x, y)
{
	this->setIDObjType(ID);

	//ID Items:
	switch (this->getIDObjType())
	{
	case 31:	//1 heart - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(1,2);
		p_item->setIndex(1);
		break;

	case 32:	//5 heart - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(2,3);
		p_item->setIndex(2);
		break;

	case 321:	//5 heart - pot
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[21]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(2,3);
		p_item->setIndex(2);
		break;

	case 33:	//Whip - pot
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[21]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(3,4);
		p_item->setIndex(3);
		break;

	case 34:	//Dagger - pot
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[21]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(5,6);
		p_item->setIndex(5);
		break;

	case 341:	//Dagger - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(5,6);
		p_item->setIndex(5);
		break;

	case 35:	//400 pts - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(10,11);
		p_item->setIndex(10);
		break;

	case 36:	//700 pts - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(11,12);
		p_item->setIndex(11);
		break;

	case 37:	//1000 pts - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(12,13);
		p_item->setIndex(12);
		break;

	case 38:	//Holy water - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(8,9);
		p_item->setIndex(8);
		break;

	case 39:	//Holy cross - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(9,10);
		p_item->setIndex(9);
		break;

	case 40:	//Stop watch - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(7,8);
		p_item->setIndex(7);
		break;

	case 41:	//Food
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_sprite->setFrame(0,1);
		p_sprite->setIndex(0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(6,7);
		p_item->setIndex(6);	
		break;

	case 42:	//Axe - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(4,5);
		p_item->setIndex(4);
		break;

	case 43:	//Break wall
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[8]->getFileName()), 125, 0);
		break;

	case 44:	//Orb - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_sprite->setFrame(0,1);	//frame an
		p_sprite->setIndex(0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(13,15);
		p_item->setIndex(13);
		break;

	case 45:	//Boomerang - candle
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[4]->getFileName()), 125, 0);
		p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);
		p_item = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_item->setFrame(15,16);
		p_item->setIndex(15);
		break;

	case 50:	//CheckPoint
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[20]->getFileName()), 125, 0);
		p_sprite->setFrame(1,2);
		p_sprite->setIndex(1);
		break;

	default:
		break;
	}


	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	pos_x = x;
	pos_y = y;

	this->pos_x_old = x;
	this->pos_y_old = y;

	veloc_y = 0;
	accel_y = 0;

	count_time = 0;

	min_X = x - 16;
	max_X = x + 16;

	//castle
	this->obj_state = Object_State;//IS_IDLE;
}

Items::~Items()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void Items::render(int cameraX, int cameraY)
{
	if (this->getIDObjType() != 50)
	{
		if (this->obj_state == IS_IDLE)
		{
			p_sprite->render(pos_x, pos_y, cameraX, cameraY);
		}
		else if (this->obj_state == IS_EXPLOSION)
		{
			p_light->render(pos_x, pos_y, cameraX, cameraY);
		}
		else if (this->obj_state == IS_ACTIVING)
		{
			p_item->render(pos_x, pos_y, cameraX, cameraY);
		}
	}
}

void Items::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch (obj_state)
	{
	case IS_IDLE:
		p_sprite->update(gameTime);
		break;

	case IS_EXPLOSION:
		count_time += gameTime;
		if (count_time > 500)
		{
			changeState(IS_ACTIVING);
		}
		p_light->update(gameTime);
		break;

	case IS_ACTIVING:
		{
			count_time += gameTime;
			if (count_time > 4000)
			{
				if (this->getIDObjType() != 44)
					changeState(IS_REMOVE);
			}

			Object* obj;
			list<Object*>::iterator i;
			width = p_item->getTexture()->getWidth();
			height = p_item->getTexture()->getHeight();

			pos_x_old = pos_x;
			pos_y_old = pos_y;

			if (this->getIDObjType() == 31)
			{
				pos_x += veloc_x * gameTime;
				if (pos_x <= min_X || pos_x >= max_X)
				{
					veloc_x *= -1;
				}
			}

			//veloc_y -= accel_y * gameTime;
			pos_y += veloc_y * gameTime;// + 1/2 * accel_y * gameTime *gameTime;



			for (i = obj_list->begin(); i != obj_list->end(); i++)
			{
				obj = *i;
				if (obj->getObjectType() == OBJECT_LAND)
				{

					Box box(this->pos_x_old,
						this->pos_y_old - this->height, 
						width, 
						height, 
						0, 
						veloc_y * gameTime);// + 1.0f/2.0f * accel_y * gameTime * gameTime);


					Box block(obj->getPosX(),
						obj->getPosY() - obj->getHeight(),
						obj->getWidth(),
						obj->getHeight()
						);

					Box boxphase = Collision::GetSweptBroadphaseBox(box);
					if (Collision::AABBCheck(boxphase, block))
					{
						float normalx = 0;
						float normaly = 0;
						float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
						if (0 < collision_time && collision_time < 1)
						{
							if (normalx == 0 && normaly == 1)
							{
								pos_y = pos_y_old + veloc_y * collision_time;
								accel_y = 0;
								veloc_y = 0;
								veloc_x = 0;
							}

						}

					}
				}//end if
			}//end for

			pos_x_old = pos_x;
			pos_y_old = pos_y;

			//update sprite
			p_item->update(gameTime);
		}
		break;
	}


}

OBJECT_TYPE Items::getObjectType()
{
	return OBJECT_ITEM;
}

void Items::changeState(int obj_state)
{
	this->obj_state = obj_state;
	switch (this->obj_state)
	{
	case IS_EXPLOSION:
		count_time = 0;
		break;

	case IS_ACTIVING:
		count_time = 0;
		if (this->getIDObjType() == 31)
		{
			veloc_x = ITEMS_VX;
		}
		veloc_y = -ITEMS_VY;
		break;
	}
}